package model

import (
	"fmt"
	"time"

	"gorm.io/gorm"
)

type ParkingLotStruct struct {
	Id            int       `gorm:"column:id" json:"id" `
	MuchId        string    `json:"much_id" form:"much_id"`
	ParkCode      string    `json:"park_code" form:"park_code"`
	Name          string    `json:"name" form:"name"`
	Address       string    `json:"address" form:"address"`
	ParkingNumber int       `json:"parking_number" form:"parking_number"`
	StopType      string    `json:"stop_type" form:"stop_type"`
	Longitude     float64   `json:"longitude" form:"longitude"`
	Latitude      float64   `json:"latitude" form:"latitude"`
	State         int       `json:"state" form:"state"`
	Sort          int       `json:"sort" form:"sort"`
	Clicks        int       `json:"clicks" form:"clicks"`
	RegionCode    string    `json:"region_code" form:"region_code"`
	RegionName    string    `json:"region_name" form:"region_name"`
	ParkCity      string    `json:"park_city" form:"park_city"`
	PType         int       `json:"p_type" form:"p_type"`
	MTime         string    `json:"m_time" form:"m_time"`
	Distance      float64   `json:"distance" form:"-" gorm:"distance"`
	FreeMinutes   int       `json:"free_minutes" form:"free_minutes"`
	CreatedAt     time.Time `gorm:"type:datetime"`
	UpdatedAt     time.Time `gorm:"type:datetime"`
}

func (c *ParkingLotStruct) TableName() string {
	return "parking_lot"
}

// 停车场列表
func (c *ParkingLotStruct) SearchAndCount(lng string, lat string, sort, start, limit, parkingType int, keywords, sortParams string) ([]ParkingLotStruct, int64, error) {
	db := Db.Table(c.TableName())

	var count int64

	var parkingLotList []ParkingLotStruct

	db = db.Debug().Select("id,much_id,park_code,name,address,parking_number,stop_type,longitude,latitude,state,sort,clicks,region_code,region_name,park_city,p_type,m_time,free_minutes,round(ST_DISTANCE(point(?,?),point(`longitude`,`latitude`)) * 111195) distance", lng, lat).Where("state = ?", 1)

	//停车类型
	if parkingType != 0 {
		db.Where("p_type = ?", parkingType)
	}

	//关键词
	if keywords != "" {
		db.Where("name like ? or address like ? ", "%"+keywords+"%", "%"+keywords+"%")
	}

	if sortParams != "" {
		switch sort {
		case 1: //综合排序
			db.Order(fmt.Sprintf("sort %s, id %s", sortParams, sortParams))
		case 2: //距离
			db.Order(fmt.Sprintf("distance %s, id desc", sortParams))
		case 3: //j
			db.Order(fmt.Sprintf("parking_number %s, id desc", sortParams))
		}
	}

	// 执行查询
	if err := db.Offset(start).Limit(limit).Scan(&parkingLotList).Error; err != nil && err != gorm.ErrRecordNotFound {
		return nil, 0, err
	}

	// 统计总数
	if err := db.Count(&count).Error; err != nil && err != gorm.ErrRecordNotFound {
		return nil, 0, err
	}

	return parkingLotList, count, nil

}

func (c *ParkingLotStruct) First(id int) (ParkingLotStruct, error) {
	var parkingLotStruct ParkingLotStruct
	err := Db.Table(c.TableName()).
		Where("id", id).
		First(&parkingLotStruct).
		Error
	if err != nil {
		return parkingLotStruct, err
	}

	return parkingLotStruct, nil
}
